package com.tywj.panda.main

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.tywj.lib.core.common.util.BarUtil
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.core.common.view.BaseFragment
import com.tywj.lib.route.Route
import com.tywj.lib.route.RouteUtil
import com.tywj.panda.customer.R
import com.tywj.panda.customer.common.provider.IDriverService
import com.tywj.panda.customer.common.route.CommonRoute
import com.tywj.panda.customer.common.view.base.FragmentChangeManager
import com.tywj.panda.customer.common.view.base.MyBaseActivity
import kotlinx.android.synthetic.main.activity_main_driver.*


/**
 * @desc 司机端主界面
 * @author June Yang
 * @date 2020/6/20
 */
@Route(path = CommonRoute.MAIN_DRIVER)
class MainDriverActivity : MyBaseActivity() {
    companion object {
        //首页
        private const val TAG_HOME = "home"

        //我的
        private const val TAG_USER = "user"
    }

    private lateinit var homeFragment: BaseFragment
    private lateinit var userFragment: BaseFragment

    //fragment集合
    private val fragments = ArrayList<BaseFragment>(2)

    //fragment tag集合
    private val tags = ArrayList<String>(2)

    private lateinit var fragmentChangeManager: FragmentChangeManager


    override fun getLayoutID(): Int = R.layout.activity_main_driver
    //设置为主界面 logout时判断
    override fun isMainActivity(): Boolean = true

    @Suppress("UNCHECKED_CAST")
    override fun initVariable(savedInstanceState: Bundle?) {
        super.initVariable(savedInstanceState)

        initFragment(savedInstanceState)

        fragmentChangeManager = FragmentChangeManager(
            supportFragmentManager,
            R.id.flMain,
            fragments as ArrayList<Fragment>,
            tags
        )
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        BarUtil.setStatusBarColor(this, ResourcesUtil.getColor(R.color.common_white))
        BarUtil.setStatusBarLightMode(this.window, true)

        //默认显示首页
        rbHome.isChecked = true
        fragmentChangeManager.currentFragmentIndex = 0

    }

    override fun initListener() {
        super.initListener()

        // 切换tab点击监听
        rgNav.setOnCheckedChangeListener { _, checkedId ->
            val preFragment = fragmentChangeManager.currentFragment

            when (checkedId) {
                R.id.rbHome -> {
                    fragmentChangeManager.currentFragmentIndex = 0
                }


                R.id.rbMine -> {
                    fragmentChangeManager.currentFragmentIndex = 1
                }
            }
            if (preFragment != fragmentChangeManager.currentFragment) {
                (preFragment as BaseFragment).onInvisible()
            }
        }
    }

    override fun onResumeExceptFirst() {
        super.onResumeExceptFirst()

        (fragmentChangeManager.currentFragment as BaseFragment).let {
            it.onVisibleEveryTime()
            it.onVisibleExceptFirst()
        }
    }


    private fun initFragment(savedInstanceState: Bundle?) {
        if (savedInstanceState == null) {
            homeFragment = RouteUtil.getServiceProvider(IDriverService::class.java)!!.getDriverHomeFragment(this)

            userFragment = RouteUtil.getServiceProvider(IDriverService::class.java)!!.getDriverUserFragment(this)
        } else {
            homeFragment = supportFragmentManager.findFragmentByTag(TAG_HOME) as BaseFragment
            userFragment = supportFragmentManager.findFragmentByTag(TAG_USER) as BaseFragment
        }
        fragments.clear()
        fragments.add(homeFragment)
        fragments.add(userFragment)

        tags.clear()
        tags.add(TAG_HOME)
        tags.add(TAG_USER)
    }
}
